package com.cskaoyan.service.wx;

import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.marketBean.MarketComment;
import com.cskaoyan.bean.marketBean.MarketCommentExample;
import com.cskaoyan.bean.marketBean.MarketUser;
import com.cskaoyan.bean.vo.zxw.wx.WxCommentCountVo;
import com.cskaoyan.bean.vo.zxw.wx.WxCommentListEntityUserInfoEntityVo;
import com.cskaoyan.bean.vo.zxw.wx.WxCommentListEntityVo;

import com.cskaoyan.mapper.marketMapper.MarketCommentMapper;
import com.cskaoyan.mapper.marketMapper.MarketUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 曾添乐啦
 * @since 2023/01/02 09:38
 */
@Service
public class WxCommentServiceImpl implements WxCommentService {

    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    MarketUserMapper userMapper;

    @Override
    public CommonData<WxCommentListEntityVo> wxCommentList(Integer valueId, Integer type, Integer limit, Integer page, Integer showType) {
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        //开启分页
        PageHelper.startPage(page, limit);

        //执行查询过程中拼接分页信息
        // 如果按照page=5 limit = 10做分页，goods.size()会是多少 10
        //使用逆向工程提供的marketStorageMapper.selectByExample()方法，根据形参知需要new一个实例对象
        List<MarketComment> marketComments = marketCommentMapper.selectMarketCommentByGoodsId(valueId);
        // List<WxCommentListEntityVo> wxCommentListEntityVos=marketCommentMapper.selectMarketCommentByGoodsId(valueId);
        //List<WxFootPrintListEntityVo> wxFootPrintListEntityVos = marketFootprintMapper.selectUserAllFootPrintByUserId(userId);
        //排序
        //example.createCriteria();该方法用来拼接查询条件,
        List<WxCommentListEntityVo> wxCommentListEntityVos = new ArrayList<>();

        for (MarketComment marketComment : marketComments) {
            //需要通过查询market_comment中的用户id（user_id），进一步去market_user查询
            Integer userId = marketComment.getUserId();
            MarketUser marketUser = userMapper.selectByPrimaryKey(userId);
            //把用户信息在封装到评论响应里面
            WxCommentListEntityUserInfoEntityVo wxCommentListEntityUserInfoEntityVo = new WxCommentListEntityUserInfoEntityVo();
            wxCommentListEntityUserInfoEntityVo.setAvatarUrl(marketUser.getAvatar());
            wxCommentListEntityUserInfoEntityVo.setNickName(marketUser.getNickname());
            WxCommentListEntityVo wxCommentListEntityVo = new WxCommentListEntityVo();
            wxCommentListEntityVo.setUserInfo(wxCommentListEntityUserInfoEntityVo);

            wxCommentListEntityVo.setAddTime(marketComment.getAddTime());
            wxCommentListEntityVo.setPicList(marketComment.getPicUrls());
            wxCommentListEntityVo.setAdminContent(marketComment.getAdminContent());
            wxCommentListEntityVo.setContent(marketComment.getContent());

            //添加到list里面
            wxCommentListEntityVos.add(wxCommentListEntityVo);

        }
        // 将查询结果作为有参构造方法的形参传入，可以获得PageInfo
        //todo:Cannot infer arguments
        PageInfo<WxCommentListEntityVo> pageInfo = new PageInfo<>(wxCommentListEntityVos);
        // PageInfo<WxFootPrintListEntityVo> pageInfo = new PageInfo<>(wxFootPrintListEntityVos);
        CommonData commonData = CommonData.data(pageInfo);
        // list()方法不做分页的情况下可以查到的数据总量

        return commonData;
    }


    /**
     * 响应数据
     * {hasPicCount: 0, allCount: 2}
     * 需要查两次数据库
     *
     * @param valueId
     * @param type
     * @return
     */
    @Override
    public WxCommentCountVo wxCommentCount(Integer valueId, Byte type) {

        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        //拼接查询条件  && !"".equals(valueId)
        if (valueId != null ) {
            criteria.andValueIdEqualTo(valueId);
        }
        //&& !"".equals(type)
        if (type != null ) {
            criteria.andTypeEqualTo(type);
        }
        //查询所有的评论
        long allCount = marketCommentMapper.countByExample(example);

        //查询所有有图的评论
        criteria.andHasPictureEqualTo(true);
        long hasPicCount = marketCommentMapper.countByExample(example);

        //封装响应数据
        WxCommentCountVo wxCommentCountVo = new WxCommentCountVo();
        wxCommentCountVo.setAllCount((int) allCount);
        wxCommentCountVo.setHasPicCount((int) hasPicCount);

        return wxCommentCountVo;
    }

    /**
     * 需要额外封装数据到响应参数中
          *      * addTime: "2023-01-02 17:31:36"*
          *      * id: 1111* @param marketComment
          *      * updateTime: "2023-01-02 17:31:36"* @return
          *      * userId: 1              在controller层封装
    */
    @Override
    public MarketComment wxCommentPost(MarketComment marketComment) {
        long currentTimeMillis = System.currentTimeMillis();
        Date date = new Date(currentTimeMillis);
        marketComment.setAddTime(date);
        marketComment.setUpdateTime(date);
        int insertAffected = marketCommentMapper.insertSelective(marketComment);
        if (insertAffected==1){
             return marketComment;
        }
        return null;
    }
}
